/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.fzhucc.modules.pms.domain;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.fzhucc.base.BaseEntity;
import com.fzhucc.modules.system.domain.User;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.Hibernate;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.Comment;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author Woolsen
 * @website https://eladmin.vip
 * @description /
 * @date 2022-11-17
 **/
@Entity
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Table(name = "pms_project")
public class Project extends BaseEntity implements Serializable {

    public static final int STATUS_REJECT = -1;
    public static final int STATUS_ING = 0;
    public static final int STATUS_PASS = 1;

    @Id
    @Column(name = "`id`")
    private String id;

    @Column(name = "`race_name`")
    @Comment(value = "赛事名称")
    private String raceName;

    @Column(name = "`race_code`")
    @Comment(value = "赛事编号")
    private String raceCode;

    @Column(name = "`race_rank`")
    @Comment(value = "赛事级别（字典）")
    private String raceRank;

    @Column(name = "delete_time")
    @ApiModelProperty(value = "假删除时间")
    private Timestamp deleteTime;

    @Column(name = "`channel_type`")
    @Comment(value = "渠道类型 1大赛 2项目申报")
    private String channelType;

    @Column(name = "`user_name`")
    @Comment(value = "参数人员名称")
    private String userName;

    @Column(name = "`user_account`")
    @Comment(value = "参数人员账号")
    private String userAccount;

    @Column(name = "`project_code`")
    @Comment(value = "项目编码")
    private String projectCode;

    @Column(name = "`project_name`", nullable = false)
    @NotBlank
    @Comment(value = "项目名称")
    private String projectName;

    @Column(name = "`project_area_code`")
    @Comment(value = "项目所在地编码")
    private String projectAreaCode;

    @Column(name = "`project_area_name`")
    @Comment(value = "项目所在地名称")
    private String projectAreaName;

    @Column(name = "`race_area`")
    @Comment(value = "赛区")
    private String raceArea;

    @Column(name = "`race_track`")
    @Comment(value = "所属赛道（字典）")
    private String raceTrack;

    @Column(name = "`race_domain`")
    @Comment(value = "所属领域")
    private String raceDomain;

    @Column(name = "`company_name`")
    @Comment(value = "申报企业")
    private String companyName;

    @Column(name = "`company_address`")
    @Comment(value = "注册地址")
    private String companyAddress;

    @Column(name = "`registration_time`")
    @Comment(value = "注册时间")
    private String registrationTime;

    @Column(name = "`main_business`")
    @Comment(value = "主营业务")
    private String mainBusiness;

    @Column(name = "`registered_capital`")
    @Comment(value = "注册资本")
    private String registeredCapital;

    @Column(name = "`business_license`")
    @Comment(value = "营业执照 统一社会信用代码")
    private String businessLicense;

    @Column(name = "`legal_person`")
    @Comment(value = "法人代表")
    private String legalPerson;

    @Column(name = "`education`")
    @Comment(value = "学历")
    private String education;

    @Column(name = "`company_people_num`")
    @Comment(value = "公司就业人数")
    private Integer companyPeopleNum;

    @Column(name = "`last_year_revenues`")
    @Comment(value = "上年经营收入")
    private String lastYearRevenues;

    @Column(name = "`is_founding_labor_union`")
    @Comment(value = "若无成立工会 是否承诺在大赛决赛日后三个月内成立工会组织")
    private String isFoundingLaborUnion;

    @Column(name = "`ownership_structure`")
    @Comment(value = "企业股权结构情况")
    private String ownershipStructure;

    @Column(name = "`declare_subject`")
    @Comment(value = "申报项目主体")
    private String declareSubject;

    @Column(name = "`address`")
    @Comment(value = "地址")
    private String address;

    @Column(name = "`principal`")
    @Comment(value = "负责人")
    private String principal;

    @Column(name = "`project_employment`")
    @Comment(value = "项目就业人数")
    private Integer projectEmployment;

    @Column(name = "`labor_union`")
    @Comment(value = "所属工会（如有）")
    private String laborUnion;

    @Column(name = "`intellectual_property`")
    @Comment(value = "核心知识产权数量及情况说明")
    private String intellectualProperty;

    @Column(name = "`project_background`")
    @Comment(value = "项目背景")
    private String projectBackground;

    @Column(name = "`description`")
    @Comment(value = "产品说明")
    private String description;

    @Column(name = "`market_prospect`")
    @Comment(value = "市场前景")
    private String marketProspect;

    @Column(name = "`recommend_channels`")
    @Comment(value = "推荐渠道")
    private String recommendChannels;

    @Column(name = "`project_img_url`")
    @Comment(value = "项目图")
    private String projectImgUrl;

    @Column(name = "`project_video_url`")
    @Comment(value = "项目视频")
    private String projectVideoUrl;

    @Column(name = "`is_model_worker`")
    @Comment(value = "是否劳模")
    private String isModelWorker;

    @Column(name = "`contacts`")
    @Comment(value = "联系人")
    private String contacts;

    @Column(name = "`contact_way`")
    @Comment(value = "联系方式")
    private String contactWay;

    @Column(name = "`contact_address`")
    @Comment(value = "通讯地址")
    private String contactAddress;

    @Column(name = "`phone`")
    @Comment(value = "联系电话")
    private String phone;

    @Column(name = "`stage_name`")
    @Comment(value = "当前阶段名称")
    private String stageName;

    @Column(name = "`stage_grade`")
    @Comment(value = "当前阶段级别")
    private Integer stageGrade;

    @Column(name = "`status`")
    @ColumnDefault("0")
    @Comment(value = "状态 -1驳回 0待审核 1审核成功")
    private Integer status;

    @Column(name = "`item_category`")
    @Comment(value = "项目类别")
    private String itemCategory;

    @Column(name = "`patent_form`")
    @Comment(value = "专利形式")
    private String patentForm;

    @Column(name = "`patent_status`")
    @Comment(value = "专利状况")
    private String patentStatus;

    @Column(name = "`patent_approval_time`")
    @Comment(value = "专利批准时间")
    private String patentApprovalTime;

    @Column(name = "`implementation`", columnDefinition = "TEXT")
    @Comment(value = "实施方式")
    private String implementation;

    @Column(name = "`project_input`")
    @Comment(value = "项目投入")
    private BigDecimal projectInput;

    @Column(name = "`annual_output`")
    @Comment(value = "年产值")
    private BigDecimal annualOutput;

    @Column(name = "`profit`")
    @Comment(value = "利润")
    private BigDecimal profit;

    @Column(name = "`first_finisher_name`")
    @Comment(value = "第一完成人姓名")
    private String firstFinisherName;

    @Column(name = "`sex`")
    @Comment(value = "性别")
    private String sex;

    @Column(name = "`job_title`")
    @Comment(value = "岗位职务")
    private String jobTitle;

    @Column(name = "`educational_level`")
    @Comment(value = "文化程度")
    private String educationalLevel;

    /**
     * 当[id]相同且[alreadyEdit]为true时，同步大赛项目则不覆盖项目
     */
    @Column(name = "`already_edit`")
    @Comment(value = "是否编辑过（用于检查同步是否覆盖）")
    private Boolean alreadyEdit;

    @Column(name = "`employer`")
    @Comment(value = "工作单位")
    private String employer;

    @Column(name = "`zip_code`")
    @Comment(value = "邮政编码")
    private String zipCode;

    @Column(name = "`del_flag`")
    @Comment(value = "删除状态(0正常 1删除)")
    private String delFlag;

    @Column(name = "`first_finisher_phone`")
    @Comment(value = "第一完成人联系电话")
    private String firstFinisherPhone;

    @Column(name = "`first_finisher_address`")
    @Comment(value = "第一完成人地址")
    private String firstFinisherAddress;

    @Column(name = "`details`", columnDefinition = "TEXT")
    @Comment(value = "项目简介")
    private String details;

    @Column(name = "`can_rate`")
    @Comment(value = "canRate")
    private String canRate;

    @Column(name = "`declare_subject_name`")
    @Comment(value = "declareSubjectName")
    private String declareSubjectName;

    @Column(name = "`level`")
    @Comment(value = "项目等级 1-职创项目库 2-优秀项目库 3-重点项目库")
    private Integer level;

    @Column(name = "`model_worker`")
    @Comment(value = "modelWorker")
    private String modelWorker;

    @Column(name = "`principal_name`")
    @Comment(value = "principalName")
    private String principalName;

    @Column(name = "`remark`")
    @Comment("备注")
    private String remark;

    @Column(name = "`score`")
    @Comment("分数")
    private String score;

    @Column(name = "`hot`")
    @Comment("热度")
    private Integer hot;

    @Comment("来源")
    @Column(name = "`source`")
    private String source;

    @ApiModelProperty(value = "地区划分 即什么地区看的到什么地区的数据,省的看到全部")
    @Column(name = "`org_area`",nullable = false)
    @NotNull
    public String orgArea;

    @Comment("用户意见是否同意展示 是否允许展示")
    @ColumnDefault("true")
    private Boolean allowShow = true;

    @Comment("是否展示")
    private Boolean isShow = false;

    @Column(name = "`is_emerging_areas`")
    @ColumnDefault("false")
    @Comment("是否属于以下新兴领域:《国家重点支持的高新技术领域》、《战略新兴产业分类》? 默认否")
    private Boolean isEmergingAreas = false;

    @Column(name = "`is_core_technology`")
    @ColumnDefault("false")
    @Comment("是否具有核心技术:已申请/授权（拥有)专利、软件著作权、集成电路布图设计权、植物新品种、新药证书、农作物品种、中药保护品种? 默认否")
    private Boolean isCoreTechnology = false;

    @Column(name = "`is_innovation_planning`")
    @ColumnDefault("false")
    @Comment("是否具有创新规划?具有创新规划、有商业计划书、项目可行性报告等? 默认否")
    private Boolean isInnovationPlanning = false;

    @Column(name = "`is_model_worker_join`")
    @ColumnDefault("false")
    @Comment("是否有劳模工匠参与? 默认否")
    private Boolean isModelWorkerJoin = false;

    @Column(name = "`is_university_student_undertaking`")
    @ColumnDefault("false")
    @Comment("是否属于大学生创业项目?  默认否")
    private Boolean isUniversityStudentUndertaking = false;

    @Column(name = "`is_auto_pass`")
    @ColumnDefault("false")
    @Comment("是否自动审核通过 默认否  上述5个字段 有一个为是 则设置为自动通过审核  直接进入职创项目库")
    private Boolean isAutoPass = false;

    @Column(name = "`sort`")
    @Comment(value = "排序")
    private Integer sort;

    @Column(name = "`intellectual_file_url`")
    @ApiModelProperty(value = "知识产权附件url")
    private String intellectualFileUrl;


    //五小添加字段
    @Column(name = "`investment`")
    @ApiModelProperty(value = "项目投入（单位：万元）")
    private String investment;

    @Column(name = "`output`")
    @ApiModelProperty(value = "项目产出（单位：万元）")
    private String output;

    @Column(name = "`last_score`")
    @ApiModelProperty(value = "最后评分")
    private String lastScore;

    @Column(name = "`awards`")
    @ApiModelProperty(value = "奖项")
    private String awards;

    @Column(name = "`report`")
    @ApiModelProperty(value = "报告附件url")
    private String report;

    @Column(name = "`send_group`")
    @ApiModelProperty(value = "发送数据时的分组 每500个数据一组 共11组")
    private Integer sendGroup;


    @Comment("所属用户")
    @OneToOne(cascade = CascadeType.DETACH)
    @JoinColumn(name = "user_id")
    private User user;

    @Comment("所属用户")
    @Column(name = "user_id", updatable = false, insertable = false)
    private Long userId;

    @ElementCollection
    @JoinTable(name = "pms_project_feature")
    private List<String> features;

    @ElementCollection
    @JoinTable(name = "pms_project_situation")
    private List<String> situations;

    @ElementCollection
    @JoinTable(name = "pms_project_appendixes")
    private Set<String> appendixes = new HashSet<>();;

    @OneToMany(cascade = CascadeType.REMOVE, orphanRemoval = true, mappedBy = "project")
    private Set<ProjectNotice> notices = new HashSet<>();

    public void copy(Project source) {
        BeanUtil.copyProperties(source, this, CopyOptions.create()
                .setIgnoreNullValue(true)
                .setIgnoreProperties("notices"));
    }

    public void setAppendixes(Set<String> appendixes) {
        this.appendixes.clear();
        this.appendixes.addAll(appendixes);
    }

    public void setNotices(Set<ProjectNotice> notices) {
        this.notices.clear();
        this.notices.addAll(notices);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
        Project project = (Project) o;
        return id != null && Objects.equals(id, project.id);
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
}
